/*
 * TStack.h
 *
 *  Created on: 09.12.2011
 *      Author: Kirill Blinov
 *      Copyright: LGPL
 */

#ifndef TSTACK_H_
#define TSTACK_H_

#include "Constants.h"

template< class ElementType  >
class TStack
{
private:
	ElementType* elements;
	UINT         stackSize;
	UINT         stackPointer;
public:
	virtual ~TStack()
	{
		delete [] elements;
	}

	TStack() : stackPointer(0), stackSize(20)
	{
		elements = new UINT[stackSize];
	};

	TStack( UINT StackSize ) : stackSize(StackSize), stackPointer(0)
	{
		elements = new ElementType[stackSize];
	};

	void Push( ElementType Element )
	{
#ifdef DEBUG_STRUCTURES
		if ( stackPointer >= stackSize )
		{
			fprintf( stderr, "[ERROR] Stack overflow!\n" );
		}
#endif
		elements[stackPointer] = Element;
		stackPointer++;
	}

	bool Pop( ElementType &Element )
	{
		bool Result = ( stackPointer > 0 );

		if ( Result )
		{
			Element = elements[stackPointer - 1];
			stackPointer--;
		}

		return Result;
	}
};

#endif /* TSTACK_H_ */
